package com.sgg.datastructure.tree;

public class HeroNode {
    private int no;
    private String name;
    private HeroNode left; //默认 null
    private HeroNode right; //默认 null

    private int leftType;//新增这2个属性 默认0 代表是左树,1代表是前驱
    private int rightType;//新增这2个属性 默认0 代表是右树,1代表是后继


    public HeroNode(int no, String name) {
        this.no = no;
        this.name = name;
    }

    public HeroNode getLeft() {
        return left;
    }

    public void setLeft(HeroNode left) {
        this.left = left;
    }

    public HeroNode getRight() {
        return right;
    }

    public void setRight(HeroNode right) {
        this.right = right;
    }

    public int getLeftType() {
        return leftType;
    }

    public void setLeftType(int leftType) {
        this.leftType = leftType;
    }

    public int getRightType() {
        return rightType;
    }

    public void setRightType(int rightType) {
        this.rightType = rightType;
    }

    @Override
    public String toString() {
        return "HeroNode{" +
                "no=" + no +
                ", name='" + name + '\'' +
                '}';
    }

    //自己,左,右
    public void preOrder() {
        System.out.println(this);
        if (getLeft() != null) {
            getLeft().preOrder();
        }
        if (getRight() != null) {
            getRight().preOrder();
        }

    }

    //左,自己,右
    public void infixOrder() {
        if (getLeft() != null) {
            getLeft().infixOrder();
        }
        System.out.println(this);
        if (getRight() != null) {
            getRight().infixOrder();
        }

    }

    //左,右,自己
    public void postOrder() {
        if (getLeft() != null) {
            getLeft().postOrder();
        }
        if (getRight() != null) {
            getRight().postOrder();
        }
        System.out.println(this);

    }

    //查找:前序,中序,后序
    public HeroNode searchByPre(int no) {
        if (this.no == no) {
            return this;
        }
        HeroNode result = null;
        if (this.left != null) {
            result = left.searchByPre(no);
        }
        if (result != null) {
            return result;
        }
        if (this.right != null) {
            result = right.searchByPre(no);
        }
        return result;
    }

    public HeroNode searchByMiddle(int no) {
        HeroNode result = null;
        if (this.left != null) {
            result = left.searchByMiddle(no);
        }
        if (result != null) {
            return result;
        }
        if (this.no == no) {
            return this;
        }
        if (this.right != null) {
            result = right.searchByMiddle(no);
        }
        return result;
    }

    public HeroNode searchByPost(int no) {
        HeroNode result = null;
        if (this.left != null) {
            result = left.searchByPost(no);
        }
        if (result != null) {
            return result;
        }

        if (this.right != null) {
            result = right.searchByPost(no);
        }
        if (result != null) {
            return result;
        }
        if (this.no == no) {
            return this;
        }
        return null;
    }

    //删除
    public void delete(int no) {
        if (this.left != null && this.left.no == no) {
            this.left = null;
            return;
        }
        if (this.right != null && this.right.no == no) {
            this.right = null;
            return;
        }
        if (this.left != null) {
            this.left.delete(no);
        }

        if (this.right != null) {
            this.right.delete(no);
        }
    }

}
